package com.budwk.app.sqfwDoc.controller;

import com.budwk.app.sqfwDoc.models.SqfwTemplateField;
import com.budwk.app.sqfwDoc.service.SqfwTemplateFieldService;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;

/**
 * @author ZY.Wang
 * @since 2023/12/5
 */
@IocBean
@At("/platform/sqfw/sqfwTemplateField")
@ApiDefinition(tag = "社区字段")
@Slf4j
public class SqfwTemplateFieldController {

    @Inject
    private SqfwTemplateFieldService sqfwTemplateFieldService;

    @At("/listPage")
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询字段")
    public Result<?> listPage(@Param("..") SqfwTemplateField dict, @Param("pageNo") Integer pageNo, @Param("pageSize") Integer pageSize) {
        Pagination pagination = sqfwTemplateFieldService.listConditionPage(dict, pageNo, pageSize);
        return Result.data(pagination);
    }

    @At("/list")
    @Ok("json")
    @POST
    @ApiOperation(name = "查询字典")
    public Result<?> list(@Param("..") SqfwTemplateField dict) {
        Cnd cnd = Cnd.NEW();
        if (StringUtils.isNotBlank(dict.getFieldName())) {
            cnd.and(SqfwTemplateField::getFieldName, "=", dict.getFieldName());
        }
        if (StringUtils.isNotBlank(dict.getFieldCode())) {
            cnd.and(SqfwTemplateField::getFieldCode, "=", dict.getFieldCode());
        }
        cnd.desc(SqfwTemplateField::getCreateTime);
        return Result.success(sqfwTemplateFieldService.query(cnd));
    }

    @At("/getDetail")
    @Ok("json")
    @POST
    @ApiOperation(name = "查询字段详情")
    public Result<?> getDetail(@Param("id") String id) {
        return Result.success(sqfwTemplateFieldService.fetch(id));
    }


    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "添加存储字段")
    public Result<?> addField(@Param("..") SqfwTemplateField data) {
        sqfwTemplateFieldService.addField(data);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改存储字段")
    public Result<?> updateField(@Param("..") SqfwTemplateField data) {
        sqfwTemplateFieldService.updateField(data);
        return Result.success();
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "删除字典原料")
    public Result<?> deleteField(@Param("id") String id) {
        sqfwTemplateFieldService.delete(id);
        return Result.success();
    }
}
